﻿using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal struct Ts
    {
        private bool _ftsItalic;
        private bool _ftsStrikeout;
        private byte[] unused;
        private byte? keepB;

        public void Read(BinaryReader reader)
        {
#if DEBUG
            ReadWriteChecker.CheckStruct((ref Ts t, BinaryReader r) => t.Read(r),
                                         (ref Ts t, BinaryWriter w) => t.Write(w), reader, ref this);

#endif
            byte b = reader.ReadByte();
            this.keepB = b;
            _ftsItalic = (b & 0x2) != 0;
            _ftsStrikeout = (b & 0x80) != 0;
            this.unused = reader.ReadBytes(3);
        }

        public void Write(BinaryWriter writer)
        {
            if (keepB != null)
                writer.Write(keepB.Value);
            else
            {


                byte b = 0;
                if (_ftsItalic)
                    b |= 0x2;
                if (_ftsStrikeout)
                    b |= 0x80;

                writer.Write(b);
            }
            if (unused == null)
                unused = new byte[3];
            writer.Write(unused);
        }
    }
}